<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    {% load static %}
    <link rel="stylesheet" href="{% static '/css/bootstrap.min.css' %}">
    <script src="{% static '/js/jquery-3.2.1.min.js' %}"></script>
    <script src="{% static '/js/bootstrap.min.js' %}"></script>
    <title>注册页面</title>
    <style>
        body {
            background-color: #eee;
        }
    </style>
    <script>
        function checkPassWord() {
            let pwd1 = $("#pwd1").val();
            let pwd2 = $("#pwd2").val();
            if (pwd1 === pwd2&&pwd1){
                $("#pwdcheck1").attr("class", "input-group col-sm-12 has-success");
                $("#pwdcheck2").attr("class", "input-group col-sm-12 has-success");
                return true;
            }else {
                $("#pwdcheck1").attr("class", "input-group col-sm-12 has-error");
                $("#pwdcheck2").attr("class", "input-group col-sm-12 has-error");
                return false;
            }
        }
        function checkusername() {
            let name = $("#username").val();
            let reg = new RegExp("[\u4e00-\u9fa5]+");
            if(reg.test(name)||!name){
                $("#namecheck").attr("class", "input-group col-sm-12 has-error");
                return false;
                }else {
                let senddata = {
                    "text":name,
                    "csrfmiddlewaretoken":"{{ csrf_token }}",
                };
                $.ajax({
                    url:"/ajax_checkname/",
                    data:senddata,
                    type:"post",
                    success:function (data) {
                        let result = data['resule'];
                        if (result === "可用"){
                            $("#namecheck").attr("class", "input-group col-sm-12 has-success");
                            return true;
                        }else {
                            $("#namecheck").attr("class", "input-group col-sm-12 has-error");
                            return false;
                        }
                    },
                });
            }
        }
        function checkPhone(){
            let phone = $("#phone").val();
            if(!(/^1\d{10}$/.test(phone))){
                $("#phonecheck").attr("class", "input-group col-sm-12 has-error");
                return false;
            }else {
                $("#phonecheck").attr("class", "input-group col-sm-12 has-success");
                return true;
            }
        }
        function checkall() {
            let flag1 = $("#namecheck").attr("class")==="input-group col-sm-12 has-success";
            let flag2 = $("#pwdcheck2").attr("class")==="input-group col-sm-12 has-success";
            if (flag1&&flag2){
                let flag3 = $("input.myCheck[name=job]:checked").val();
                return !!flag3;
            }else {
                return false;
            }
        }
    </script>
</head>
<body>

<div class="container">
    <div class="row">
        <div class="col-md-4 col-md-offset-4" style="margin-top: 100px">
            <h1 class="text-center">请注册</h1>
            <form class="form-horizontal" action="" method="post" onsubmit="return checkall()">
                {% csrf_token %}
                <div class="form-group">
                    <div class="input-group col-sm-12" id="namecheck">
                        <span class="input-group-addon"><label for="username">英文昵称</label></span>
                        <input type="text" name="username" class="form-control" id="username" placeholder="username" required="required" onblur="checkusername()">
                    </div>
                </div>
                <div class="form-group">
                    <div class="input-group col-sm-12" id="pwdcheck1">
                        <span class="input-group-addon"><label for="pwd1">输入密码</label></span>
                        <input type="password" name="pwd" class="form-control" id="pwd1" placeholder="password" required="required" onblur="checkPassWord()">
                    </div>
                </div>
                <div class="form-group">
                    <div class="input-group col-sm-12" id="pwdcheck2">
                        <span class="input-group-addon"><label for="pwd2">确认密码</label></span>
                        <input type="password" class="form-control" id="pwd2" placeholder="checkpassword" required="required" onblur="checkPassWord()">
                    </div>
                </div>
                <div class="form-group">
                    <div class="input-group col-sm-12">
                        <span class="input-group-addon"><label for="name">真实姓名</label></span>
                        <input type="text" name="name" class="form-control" id="name" placeholder="name" required="required">
                    </div>
                </div>
                <div class="form-group">
                    <div class="input-group col-sm-12" id="phonecheck">
                        <span class="input-group-addon"><label for="phone">手机号码</label></span>
                        <input type="text" name="phone" class="form-control" id="phone" placeholder="phone" required="required" onblur="checkPhone()">
                    </div>
                </div>
                <div class="form-group">
                    <div class="input-group col-sm-offset-2">
                        <div class="checkbox">
                            <label>
                                <input class="myCheck" type="radio" name="job" value="0"> 后厨
                                <input class="myCheck" type="radio" name="job" value="1"> 前台
                                <input class="myCheck" type="radio" name="job" value="2"> 服务生
                                <input class="myCheck" type="radio" name="job" value="3"> 老板
                            </label>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="input-group col-sm-offset-3 col-sm-6">
                        <button type="submit" class="btn btn-primary btn-block">立即注册</button>
                        <span>{{ msg }}</span>
                    </div>
                </div>
            </form>
            <div class="form-group">
                <div class="input-group col-sm-offset-3 col-sm-6">
                    <a class="btn btn-primary btn-block" href="/login/">登录页面</a>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>